package com.ny.zmb.openapi.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ny.zmb.openapi.ysutil.from.BizReqJsonCollege.BizReqJsonSeparateAccounts;
import com.ny.zmb.openapi.ysutil.utils.RequestNoUntil;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@TableName("pm_order_split_account_log")
@AllArgsConstructor
@Data
public class PmOrderSplitAccountLog implements Serializable {

    private static final long serialVersionUID = 1L;
    @TableId(type = IdType.AUTO)
    private Long id;
    /**
     * 原订单金额，单位：元
     */
    private String amount;
    /**
     * 分账明细列表，是否分账为Y时，此参数必传
     */
    private String divisionList;
    /**
     * 分账模式，01-比例、02-金额
     */
    private String divisionMode;
    /**
     * 原订单是否分账，Y-是
     * N-否（无需分账订单传N，直接入账到收款方
     */
    private String isDivision;
    /**
     * 分账结果异步通知地址，为空则不通知
     */
    private String notifyUrl;
    /**
     * 需分账订单的业务请求号
     */
    private String origRequestNo;
    /**
     * 收款方银盛商户号
     */
    private String payeeMerchantNo;
    /**
     * 商户系统生成的订单号，商户系统生成的订单号，须保证在商户端不重复，生成规则前8位必须为交易日期，如20220525，范围跨度支持包含当天在内的前后一天，且只能由大小写英文字母、数字、下划线及横杠组成，示例值：202205256843192280647118
     */
    private String requestNo;
    /**
     * 创建时间
     */
    private Date createdDate;
    /**
     * 更新时间
     */
    private Date updatedDate;

    /**
     * -1 分账失败
     * 1 分账成功（分账资金已入账分账方）
     * 2 预分账成功（可视为分账成功，分账资金在T+1后入账到分账方结算账户）
     * 3 受理成功（处理中，不表示分账成功）
     * 999 未知状态，请重新查询
     */
    private int status;
    /**
     * 备注
     */
    private String remarks;

    // 对象转换
    public PmOrderSplitAccountLog(BizReqJsonSeparateAccounts obj) {
        if (obj == null) throw new RuntimeException("空异常");
        if (obj.getRequestNo() == null || obj.getRequestNo().isEmpty()) {
            obj.setRequestNo(RequestNoUntil.generateNumericTaskNumber());
        }
        this.requestNo = obj.getRequestNo();
        this.payeeMerchantNo = obj.getPayeeMerchantNo();
        this.origRequestNo = obj.getOrigRequestNo();
        this.notifyUrl = obj.getNotifyUrl();
        if (obj.getDivisionList() != null) {
            this.divisionList = obj.getDivisionList().toString();
        } else {
            this.divisionList = "";
        }
        this.amount = obj.getAmount();
        this.isDivision = obj.getIsDivision();
        this.status = 0;
        this.create();
    }

    public PmOrderSplitAccountLog() {
        this.createdDate = new Date();
    }

    public PmOrderSplitAccountLog create() {
        this.createdDate = new Date();
        this.status = 0;
        return this;
    }

    /**
     * @param status
     * @param remarks
     * @return
     */
    public PmOrderSplitAccountLog update(String status, String remarks) {
        this.getStatusByMsg(status);
        this.setRemarks(remarks);
        return this.update();
    }

    /**
     * @param remarks
     * @return
     */
    public PmOrderSplitAccountLog update(int status, String remarks) {
        this.setRemarks(remarks);
        this.status = status;
        return this.update();
    }

    public PmOrderSplitAccountLog update(int status) {
        this.status = status;
        return this.update();
    }

    public PmOrderSplitAccountLog update() {
        this.updatedDate = new Date();
        return this;
    }

    /**
     * 判断
     *
     * @param msg
     * @return
     */
    public PmOrderSplitAccountLog getStatusByMsg(String msg) {
        switch (msg) {
            case "ACCEPTED":
                this.status = 3;
                break;
            case "SPLIT_SUCCESS":
                this.status = 2;
                break;
            case "SUCCESS":
                this.status = 1;
                break;
            case "FAILED":
                this.status = -1;
                break;
            default:
                this.status = -999;
        }
        return this;
    }
}
